package vn.edu.uit.chemistry;

import java.util.ArrayList;
import java.util.Map;

import vn.edu.uit.chemistry.model.Nhom;
import vn.edu.uit.chemistry.model.NhomAdapter;
import vn.edu.uit.chemistry.model.ThongTinHocSinh;
import vn.edu.uit.chemistry.model.ThongTinHocSinhAdapter;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import vn.edu.uit.chemistry.QRCode.IntentIntegrator;
import vn.edu.uit.chemistry.QRCode.IntentResult;


public class ThietLap extends Activity {

	ImageView imgQRCode;
	Button btnDiemDanh;
	Button btnNhom;
	Button btnQuetQR;
	
	String path = Environment.getExternalStorageDirectory().getAbsolutePath()
			.toString()
			+ "/hoa hoc/qrcode.png";
	
	public String hoten = "";
	public String msgv = "";
	public String ip = "";
	public String port = "";
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.screen_thiet_lap);
		
		imgQRCode = (ImageView) findViewById(R.id.imgQRCode);
		imgQRCode.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				final Dialog dialog = new Dialog(ThietLap.this);
				dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
				dialog.setContentView(R.layout.dialog_thong_tin_socket);
				dialog.setCancelable(true);

				TextView tvHoten = (TextView) dialog.findViewById(R.id.tvHoten);
				tvHoten.setText(hoten);
				
				TextView tvMSGV = (TextView) dialog.findViewById(R.id.tvMSGV);
				tvMSGV.setText(msgv);
				
				TextView tvIP = (TextView) dialog.findViewById(R.id.tvIP);
				tvIP.setText(ip);
				
				TextView tvPort = (TextView) dialog.findViewById(R.id.tvPort);
				tvPort.setText(port);

				Button btnOK = (Button) dialog.findViewById(R.id.btnOK);
				btnOK.setOnClickListener(new OnClickListener() {

					public void onClick(View v) {
						dialog.dismiss();
					}
				});

				dialog.show();
			}
		});
		
		btnDiemDanh = (Button) findViewById(R.id.btnDSHS);
		btnDiemDanh.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				
				Context ctx = ThietLap.this;
				
				ArrayList<ThongTinHocSinh> listThongTinHocSinh = new ArrayList<ThongTinHocSinh>();
				SharedPreferences prefs = ctx.getSharedPreferences("DSHS", 0);
				Map<String, ?> keys = prefs.getAll();
				for (Map.Entry<String, ?> entry : keys.entrySet()) {
					ThongTinHocSinh tths = new ThongTinHocSinh();
					tths.setMshs(entry.getKey());
					String[] values = entry.getValue().toString().split("#");
					tths.setTenHS(values[0]);
					if (values[1].length() > 1) {
						tths.setAbsent(false);
					} else {
						tths.setAbsent(true);
					}
					
					listThongTinHocSinh.add(tths);
				}
				
				final Dialog dialog = new Dialog(ctx);
				dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
				dialog.setContentView(R.layout.dialog_diem_danh);
				dialog.setCancelable(true);

				ListView lvDiemDanh = (ListView) dialog.findViewById(R.id.lvDiemDanh);
				lvDiemDanh.setAdapter(new ThongTinHocSinhAdapter(ctx, listThongTinHocSinh));
				
				Button btnOK = (Button) dialog.findViewById(R.id.btnOK);
				btnOK.setOnClickListener(new OnClickListener() {

					public void onClick(View v) {
						dialog.dismiss();
					}
				});

				dialog.show();
				
			}
		});
		
		btnNhom = (Button) findViewById(R.id.btnNhom);
		btnNhom.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				Context ctx = ThietLap.this;
				
				ArrayList<Nhom> listNhom = new ArrayList<Nhom>();
				SharedPreferences prefs = ctx.getSharedPreferences("DSNhom", 0);
				Map<String, ?> keys = prefs.getAll();
				for (Map.Entry<String, ?> entry : keys.entrySet()) {
					Nhom nhom = new Nhom();
					nhom.setHost(entry.getKey());
					String[] values = entry.getValue().toString().split("#");
					nhom.setTenNhom(values[0]);
					listNhom.add(nhom);
				}
				
				
				final Dialog dialog = new Dialog(ctx);
				dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
				dialog.setContentView(R.layout.dialog_nhom);
				dialog.setCancelable(true);
				
				TextView tvThongBao = (TextView) dialog.findViewById(R.id.tvTB);
				ListView lvNhom = (ListView) dialog.findViewById(R.id.lvNhom);

				if (listNhom.size() == 0 || listNhom == null) {
					tvThongBao.setVisibility(View.VISIBLE);
					lvNhom.setVisibility(View.GONE);
				} else {
					tvThongBao.setVisibility(View.GONE);
					lvNhom.setVisibility(View.VISIBLE);
					lvNhom.setAdapter(new NhomAdapter(ctx, listNhom));
				}
				
				Button btnOK = (Button) dialog.findViewById(R.id.btnOK);
				btnOK.setOnClickListener(new OnClickListener() {

					public void onClick(View v) {
						dialog.dismiss();
					}
				});

				dialog.show();
			}
		});
		
		btnQuetQR = (Button) findViewById(R.id.btnQuetQRCode);
		btnQuetQR.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				IntentIntegrator integrator = new IntentIntegrator(
						ThietLap.this);
				integrator.initiateScan();
			}
		});
	}

	@Override
	protected void onResume() {
		super.onResume();
		imgQRCode.setImageBitmap(BitmapFactory.decodeFile(path));
		
		SharedPreferences prefs = getSharedPreferences("TaiKhoanGV", 0);
		hoten = prefs.getString("hoten", "");
		msgv = prefs.getString("msgv", "");
		ip =  prefs.getString("ip", "");
		port =  prefs.getString("port", "");
		
//		prefs = getSharedPreferences("DSNhom", 0);
//		SharedPreferences.Editor editor = prefs.edit();
//		editor.putString("host", "ten#0");
//		editor.commit();
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		IntentResult scanResult = IntentIntegrator.parseActivityResult(
				requestCode, resultCode, intent);

		if (scanResult.getRawBytes() != null) {
			String barcode;
			barcode = scanResult.getContents();

		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			return false;
		}
		return false;
	}
}
